package yhao.infra.feature.alipay.core.request.pay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import yhao.infra.feature.alipay.core.request.BasePayRequest;

/**
 * 统一收单线下交易预创建
 * @see <a href="https://docs.open.alipay.com/api_1/alipay.trade.precreate/">更多细节</a>
 */
public class PayPreCreateRequest extends BasePayRequest<AlipayTradePrecreateResponse> {
    private String timeout_express;//取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天

    public PayPreCreateRequest(String out_trade_no, String total_amount,
                               String subject, String timeout_express, String url) {
        super(out_trade_no, total_amount, subject,url);
        this.timeout_express = timeout_express;
    }

    public String getTimeout_express() {
        return timeout_express;
    }

    @Override
    protected AlipayTradePrecreateResponse doRequest(){
        String bizContent = JSONObject.toJSONString(this);

        AlipayTradePrecreateRequest aliPayRequest = new AlipayTradePrecreateRequest();//创建API对应的request
        aliPayRequest.setNotifyUrl(alipayConfig.getNotifyUrlPre()+this.getNotifyUrl());//在公共参数中设置回跳和通知地址
        aliPayRequest.setBizContent(bizContent);//填充业务

        String method = logRequest(aliPayRequest);
        try {
            return alipayClient.execute(aliPayRequest); //调用SDK生成表单
        } catch (AlipayApiException e) {
            logger.error(method+getOut_trade_no(), e.getMessage());
        }
        return null;
    }
}
